﻿using UnityEngine;
using System.Collections;

public class Champion : Card {

	public int attack;
	public int abilityPower;
	public float health;
	public int lifesteal;
	[HideInInspector]public float maxHealth;
	[HideInInspector]public int ultCoolDown;
	[HideInInspector]public float basicAbilityScalingAd;
	[HideInInspector]public float basicAbilityScalingAp;
	[HideInInspector]public float ultAbilityScalingAd;
	[HideInInspector]public float ultAbilityScalingAp;
	[HideInInspector]public string basicAbilityDescription;
	[HideInInspector]public string ultimateAbilityDescription;
	[HideInInspector]public int attackRange = 1;

	public bool isDead;
	public bool canMove;
	public bool canAttack;
	public bool isTargettable;
	public bool inPlay;
	[HideInInspector]public bool moveMenuOpen;
	//[HideInInspector]public bool viewMenuOpen;
	[HideInInspector]public int numMoves;
	[HideInInspector]public int numMovesThisTurn;
	[HideInInspector]public bool attackMenuOpen;
	[HideInInspector]public int numAttacks = 1;
	[HideInInspector]public int numAttacksThisTurn;
	[HideInInspector]public bool drawButtonsOnTargets;

	public GridCell myGridCell;

	[HideInInspector]public Item[] equippedItems = new Item[15];
	public int currentEquippedItemNumber = 0;
	public string equippedItemsNames;

	//summoner spell effects
	public int numTurnsIgnited;
	public int numTurnsExhausted;
	public int numGhostMoves;

	//public Texture foregroundHealth;
	//public Texture backgroundHealth;
	//public float foregroundHealthLength;

	public void initializeChampionStuff(){
		myType = type.champion;
	}

	// Use this for initialization
	void Start () {
		canMove = true;
		canAttack = true;
		isTargettable = true;
		isDead = false;
		GameController.gc.playerOneNexus.Health = 100;
		//foregroundHealth = GameController.gameController.foregroundHealth;
		//backgroundHealth = GameController.gameController.backgroundHealth;
		//foregroundHealthLength = 50;
	}

	public void useBasicAbility(){
		if(isDead || !canAttack){
			return;
		}
	}

	public void useUltimateAbility(){
		if(isDead || !canAttack){
			return;
		}
	}

	public void useAttack(Champion attackTarget){
		if(isDead || !canAttack){
			return;
		}
		if(numTurnsExhausted>0)
			attackTarget.health-=attack/2;
		else
			attackTarget.health-=attack;
		health+=lifesteal;
		if(health > maxHealth)
			health = maxHealth;
		drawButtonsOnTargets = false;
		StartCoroutine(GameController.gc.waitToStopDoingSomethingElse());
		numAttacksThisTurn++;
		checkIfDead(attackTarget);
		GameController.gc.updateLastAction(cardName+" attacked "+attackTarget.cardName);
	}

	public void useAttack(Nexus nexus){
		if(isDead||!canAttack)
			return;
		if(nexus.numTurnsFortified > 0)
			return;
		if(numTurnsExhausted>0)
			nexus.Health-=attack/2;
		else
			nexus.Health-=attack;
		drawButtonsOnTargets = false;
		StartCoroutine(GameController.gc.waitToStopDoingSomethingElse());
		numAttacksThisTurn++;
		if(nexus.Health<=0){
			GameController.gc.gameOver();
		}
		GameController.gc.updateLastAction(cardName+" attacked Nexus");
	}

	public void checkIfDead(Champion attackTarget){
		if(attackTarget.health<=0){
			attackTarget.myGridCell.hasCard = false;
			attackTarget.myGridCell.card = null;
			attackTarget.myGridCell = null;
			for(int i = 0; i < GameController.gc.champions.Length; i++){
				if(GameController.gc.champions[i] == attackTarget)
					GameController.gc.champions[i] = null;
			}
			attackTarget.sendToGraveyard();
		}
	}

	public void getMoveTarget(){

	}

	public void move(int xAmount, int yAmount){
		if(isDead || !canMove){
			return;
		}
		myGridCell.hasCard = false;
		myGridCell.card = null;
		myGridCell = GameController.gc.board[(int) myGridCell.gridPosition.x + xAmount, (int) myGridCell.gridPosition.y + yAmount];
		transform.position = new Vector3(myGridCell.transform.position.x, myGridCell.transform.position.y, transform.position.z);
		myGridCell.hasCard = true;
		myGridCell.card = this;
		moveMenuOpen = false;
		numMovesThisTurn++;
		closeMenu();
		GameController.gc.isInMenu = false;
		GameController.gc.updateLastAction(cardName+" moved");
	}
	
	public void openAttackMenu(){
		attackMenuOpen = true;
	}

	public void openAbilityMenu(){
		
	}

	public void openViewMenu(){
		viewMenuOpen = true;
	}
	

	void OnGUI(){
		if(!GameController.gc.gameover){
			if(viewMenuOpen){
				GUI.Box(new Rect(Screen.width * 1/5,Screen.height * 1/10,Screen.width * 4/5,Screen.height * 4/5), GameController.gc.blackBackgroud);
				GUI.Box(new Rect(Screen.width * 1/5,Screen.height * 3/10,Screen.width * 4/5,Screen.height * 4/5), GameController.gc.blackBackgroud);
				GUI.DrawTexture(new Rect(Screen.width * 2/10 + 10,Screen.height * 2/10,Screen.width * 1/5,Screen.height * 2/5), cardSprite);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 2/10,Screen.width * 6/10,Screen.height * 1/20),"Champion Name: "+ cardName);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 5/20,Screen.width * 6/10,Screen.height * 1/20),"Remaining Champion Health: "+ health);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 6/20,Screen.width * 5/10,Screen.height * 1/20),"Champion Attack Power: "+ attack);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 7/20,Screen.width * 5/10,Screen.height * 1/20),"Champion Ability Power: "+ abilityPower);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 8/20,Screen.width * 5.5f/10,Screen.height * 2.5f/20),"Champion Basic Ability Description: "+ basicAbilityDescription);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 11/20,Screen.width * 5.5f/10,Screen.height * 2.5f/20),"Champion Ultimate Ability Description: "+ ultimateAbilityDescription);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 14/20,Screen.width * 5/10,Screen.height * 2/20),"Champion Description: "+ cardDescription);
				GUI.Label(new Rect(Screen.width * 4.2f/10,Screen.height * 16/20,Screen.width * 5/10,Screen.height * 2/20),"Equipped Items: "+ equippedItemsNames);
				if(GUI.Button(new Rect(Screen.width * 1/5, Screen.height * 16/20, Screen.width * 1/10, Screen.height * 1/10), "Close"))
					viewMenuOpen = false;
			}
			//draw targets on potential attack targets
			else if(drawButtonsOnTargets){
				for(int i = 0; i < 4; i++){
					for(int j = 0; j < 4; j++){
						GridCell targetGridCell = GameController.gc.board[i,j];
						if(targetGridCell.hasCard && (Mathf.Abs(myGridCell.gridPosition.x-i)+Mathf.Abs(myGridCell.gridPosition.y-j)<=attackRange)){
							if(!targetGridCell.card.player1Card){
								Vector3 cardPosition = Camera.main.WorldToScreenPoint(targetGridCell.card.transform.position);
								if (GUI.Button(new Rect(cardPosition.x-GameController.gc.cardSize.x/2, Screen.height - GameController.gc.cardSize.y/2 - cardPosition.y, GameController.gc.cardSize.x, GameController.gc.cardSize.y), "")){
									Champion attackTarget = targetGridCell.card;
									useAttack(attackTarget);
								}
							}
						}
					}
				}
				if((myGridCell.gridPosition.x - attackRange) <= -1){
					Vector3 position = Camera.main.WorldToScreenPoint(GameController.gc.playerTwoNexus.transform.position);
					if(GUI.Button(new Rect(position.x-35,Screen.height - position.y-50, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "")){
						useAttack(GameController.gc.playerTwoNexus);
					}
				}
				if(GUI.Button(new Rect(0,Screen.height-50, 100, 50), "Cancel")){
					drawButtonsOnTargets = false;
				}
			}

			else if (attackMenuOpen){
				if (GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 - 50, buttonWidth, buttonHeight), "Basic Ability")){

				}
				if(GUI.Button(new Rect(Screen.width/2 - 225, Screen.height/2, buttonWidth, buttonHeight), "Ultimate Ability")){

				}
				if(GUI.Button(new Rect(Screen.width/2 + 75, Screen.height/2, buttonWidth, buttonHeight), "Basic Attack")){
					drawButtonsOnTargets = true;
					attackMenuOpen = false;
					GameController.gc.doingSomethingElse = true;
				}
				if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 + 50, buttonWidth, buttonHeight), "Back")){
					attackMenuOpen = false;
				}
			}

			//code for the movement gui menu
			else if(moveMenuOpen){
				//move up
				if(myGridCell.gridPosition.x > 0 && !GameController.gc.board[(int) myGridCell.gridPosition.x-1, (int) myGridCell.gridPosition.y].hasCard)
					if (GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 - 100, buttonWidth, buttonHeight), "Up"))
						move (-1,0);
				//move down
				if(myGridCell.gridPosition.x < 3 && !GameController.gc.board[(int) myGridCell.gridPosition.x+1, (int) myGridCell.gridPosition.y].hasCard)
					if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 + 100, buttonWidth, buttonHeight), "Down"))
						move (1,0);
				//move right
				if(myGridCell.gridPosition.y < 3 && !GameController.gc.board[(int) myGridCell.gridPosition.x, (int) myGridCell.gridPosition.y+1].hasCard)
					if(GUI.Button(new Rect(Screen.width/2 + 75, Screen.height/2, buttonWidth, buttonHeight), "Right"))
						move (0,1);
				//move left
				if(myGridCell.gridPosition.y > 0 && !GameController.gc.board[(int) myGridCell.gridPosition.x, (int) myGridCell.gridPosition.y-1].hasCard)
					if(GUI.Button(new Rect(Screen.width/2 - 225, Screen.height/2, buttonWidth, buttonHeight), "Left"))
						move (0,-1);
				//go back to previous menu
				if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2, buttonWidth, buttonHeight), "Back"))
					moveMenuOpen = false;
			}
			//code for the basic options menu
			else if(menuOpen){
				if(player1Card){
					if(numMoves > numMovesThisTurn && !isDead && canMove)
						if (GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 - 50, buttonWidth, buttonHeight), "Move"))
							moveMenuOpen = true;
					if(canAttack && numAttacks > numAttacksThisTurn)
						if(GUI.Button(new Rect(Screen.width/2 + 75, Screen.height/2, buttonWidth, buttonHeight), "Attack"))
							openAttackMenu();
				}
				if(GUI.Button(new Rect(Screen.width/2 - 225, Screen.height/2, buttonWidth, buttonHeight), "View"))
					openViewMenu();
				if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 + 50, buttonWidth, buttonHeight), "Close"))
					closeMenu();
			}
			//GUILayout.Label (cardName + "'s Remaining Health:");
			//GUI.DrawTexture(new Rect(160,0,50,25), backgroundHealth);
			//GUI.DrawTexture(new Rect(160,0,foregroundHealthLength,25), foregroundHealth);
		}
	}
}
